package com.hw.ui.login;

import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.hw.HWApp;
import com.hw.R;
import com.hw.base.activity.BaseActivity;
import com.hw.base.util.Util;
import com.hw.daobean.DataBaseControl;
import com.hw.daobean.RelationUpInfo;
import com.hw.ui.config.ConfigActivity;
import com.hw.ui.main.MainActivity;
import com.hw.ui.scanfconfig.ScanfConfigActivity;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity<LoginModel, LoginView, LoginPresent> implements LoginView {
    private String tag = "LoginActivity";
    @Bind(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tvright)
    TextView tvright;
    @Bind(R.id.ivu)
    ImageView ivu;
    @Bind(R.id.login_id)
    EditText loginId;
    @Bind(R.id.ivp)
    ImageView ivp;
    @Bind(R.id.login_p)
    EditText loginP;
    @Bind(R.id.cb_rp)
    CheckBox cbRp;
    @Bind(R.id.loginbt)
    Button loginbt;
    @Bind(R.id.configbt)
    Button configbt;
    @Bind(R.id.iv)
    ImageView iv;
    @Bind(R.id.configcodebt)
    Button configcodebt;
    @Bind(R.id.tvr)
    TextView tvr;
    boolean flag = false;
    String cid, user, pw;

    public static String getVersionName(Context mContext) {
        String versionName = "0.0";
        try {
            versionName = mContext.getPackageManager().
                    getPackageInfo(mContext.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } finally {
            return versionName;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        tvTitle.setText("登录");
        tvr.setText("版本号:v" + getVersionName(this));
        flag = HWApp.sSharedPreferences.getBoolean(Util.rps, false);
        cbRp.setChecked(flag);
        loginId.setText(HWApp.sSharedPreferences.getString(Util.userk, ""));
        loginP.setText(HWApp.sSharedPreferences.getString(Util.pwk, ""));
//        loginId.setText(HWApp.sSharedPreferences.getString(Util.userk, "zh01"));
//        loginP.setText(HWApp.sSharedPreferences.getString(Util.pwk, "123456"));
    }

    @Override
    public LoginModel createModel() {
        return new LoginModelImpl();
    }

    @Override
    public LoginView createView() {
        return this;
    }

    @Override
    public LoginPresent createPresenter() {
        return new LoginPresent();
    }

    @Override
    public void login() {
        showMsg("登录成功");
        if (flag) {
            HWApp.sEditor.putBoolean(Util.rps, true);
            HWApp.sEditor.putString(Util.userk, user);
            HWApp.sEditor.putString(Util.pwk, pw);
            HWApp.sEditor.commit();
        } else {
            HWApp.sEditor.putBoolean(Util.rps, false);
            HWApp.sEditor.putString(Util.userk, "");
            HWApp.sEditor.putString(Util.pwk, "");
            HWApp.sEditor.commit();
        }
        presenter.checkDelete();
        intentToActivityWithoutParameter(this, MainActivity.class);
        finish();
    }

    @OnClick({R.id.iv_title_left, R.id.cb_rp, R.id.loginbt, R.id.configbt, R.id.configcodebt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.cb_rp:
                flag = !flag;
                break;
            case R.id.loginbt:
                if (HWApp.sSharedPreferences.getString(Util.cidk, "").length() == 0) {
                    showMsg("请配置企业编号");
                    return;
                }
                if (HWApp.sSharedPreferences.getInt(Util.boxmixlen, 0) == 0) {
                    showMsg("请完成扫描配置");
                    return;
                }
                cid = HWApp.sSharedPreferences.getString(Util.cidk, "");
                user = loginId.getText().toString();
                pw = loginP.getText().toString();
                if (user.length() == 0) {
                    showMsg("请填写用户名");
                    return;
                }
                if (pw.length() == 0) {
                    showMsg("请填写密码");
                    return;
                }
                presenter.login(user, pw);
                break;
            case R.id.configbt:
                intentToActivityWithoutParameter(this, ConfigActivity.class);
                break;
            case R.id.configcodebt:
                intentToActivityWithoutParameter(this, ScanfConfigActivity.class);
                break;
        }
    }
}
